package com.run.web.backend;


import com.github.pagehelper.Page;
import com.run.mapper.EbProductCategoryMapper;
import com.run.model.EbProductCategory;
import com.run.service.ProductClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller     //标记为web服务员
@RequestMapping("/productClass")//工号
public class ProductClassController {
    @Autowired
    private ProductClassService productCategoryService;//从Service层调用

    @Autowired
    private EbProductCategoryMapper ebProductCategoryMapper;
    //1.商品的分类（大小类）查询
    @RequestMapping("/list")
    public String list(Model model){
        //查询所有大类和小类
        //1.调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap=productCategoryService.findBigAndSmallCategory();
        //2.通过model传递给jsp
        model.addAttribute("categoryMap",categoryMap);
        return "manage/productClass";
    }

    @RequestMapping("/add")
    public String add(Model model,EbProductCategory ebProductCategory){
        //查询所有大类和小类
        //1.调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap=productCategoryService.findBigAndSmallCategory();
        //2.通过model传递给jsp
        model.addAttribute("categoryMap",categoryMap);

        return "manage/productClass-add";
    }

    @RequestMapping("/modify")
    public String modify(Model model,Integer epcId){
        EbProductCategory ebProductCategory = productCategoryService.selectOne(epcId);
        model.addAttribute("ebProductCategory",ebProductCategory);
        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findBigAndSmallCategory();
        //2.通过model传递给jsp
        model.addAttribute("categoryMap",categoryMap);
        return "manage/productClass-modify";
    }

    @RequestMapping("/edit")
    public String edit(EbProductCategory ebProductCategory) {
        Integer result = productCategoryService.updateEbProductCategory(ebProductCategory);
        System.out.println(result);
        if (result <=0) {
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/save")
    public String save(EbProductCategory ebProductCategory){
        Integer result = productCategoryService.save(ebProductCategory);
        if(result<0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete")
    public String delete(Model model,@RequestParam("epcId")Integer epcId){
        Integer result = productCategoryService.delete(epcId);
        model.addAttribute("result",result);
        System.out.println(result);
        if (result <=0) {
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
}

